home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / applet / communication / example / TalkServer.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  8.8 KB  |  269 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.net.*;
  18. import java.io.*;
  19.  
  20. class TalkServer {
  21.     TalkServerThread[] tstList = new TalkServerThread[2];
  22.     boolean DEBUG = false;
  23.  
  24.     public static void main(String[] args) {
  25.         new TalkServer().start();
  26.     }
  27.  
  28.     public void start() {
  29.         ServerSocket serverRSocket = null;
  30.         int numConnected = 0;
  31.  
  32.         try {
  33.             serverRSocket = new ServerSocket(0);
  34.             System.out.println("TalkServer listening on rendezvous port: "
  35.                                + serverRSocket.getLocalPort());
  36.         } catch (IOException e) {
  37.             System.err.println("Server could not create server socket for rendezvous.");
  38.             return;
  39.         }
  40.  
  41.         while (true) {
  42.  
  43.             //Connect to two clients.
  44.             while (numConnected < 2) {
  45.                 TalkServerThread tst;
  46.                 tst = connectToClient(serverRSocket);
  47.                 if (tst != null) {
  48.                     numConnected++;
  49.                     if (tstList[0] == null) {
  50.                         tstList[0] = tst;
  51.                     } else {
  52.                         tstList[1] = tst;
  53.                     }
  54.                 }
  55.             } //end while (numConnected < 2) loop
  56.  
  57.             if (DEBUG) {
  58.                 try {
  59.                     System.out.println("tst #0 = " + tstList[0]);
  60.                 } catch (Exception e) {}
  61.                 try {
  62.                     System.out.println("tst #1 = " + tstList[1]);
  63.                 } catch (Exception e) {}
  64.             }
  65.  
  66.             //If they're really OK, tell them to start writing.
  67.             if (everythingIsOK(0) & everythingIsOK(1)) {
  68.                 for (int i = 0; i < 2; i++) {
  69.                     writeToStream("START WRITING!\n----------------------"
  70.                                   + "-------------", tstList[i].os);
  71.                 }
  72.             } else {
  73.                 System.err.println("2 server threads created, but "
  74.                                    + "not everything is OK");
  75.             }
  76.  
  77.             while (numConnected == 2) {
  78.                 if (!everythingIsOK(0)) {
  79.                     if (DEBUG) {
  80.                         System.out.println("Applet #0 is hosed; disconnecting.");
  81.                     }
  82.                     numConnected--;
  83.                     cleanup(tstList[0]);
  84.                     tstList[0] = null;
  85.                 }
  86.                 if (!everythingIsOK(1)) {
  87.                     if (DEBUG) {
  88.                         System.out.println("Applet #1 is hosed; disconnecting.");
  89.                     }
  90.                     numConnected--;
  91.                     cleanup(tstList[1]);
  92.                     tstList[1] = null;
  93.                 }
  94.                        try {
  95.                     Thread.sleep(1000);
  96.                 } catch (InterruptedException e) {
  97.                 } 
  98.             } //end while(numConnected==2) loop
  99.  
  100.             if (DEBUG) {
  101.                 try {
  102.                     System.out.println("Number of connections = " + numConnected);
  103.                     System.out.println("tst #0 = " + tstList[0]);
  104.                     System.out.println("tst #1 = " + tstList[1]);
  105.                 } catch (Exception e) {}
  106.             }
  107.  
  108.         } //end while (true) loop
  109.     }
  110.  
  111.     protected TalkServerThread connectToClient(ServerSocket serverRSocket) {
  112.  
  113.         DataOutputStream os = null;
  114.         Socket rendezvousSocket = null;
  115.         TalkServerThread tst = null;
  116.  
  117.         //Listen for client connection on the rendezvous socket.
  118.         try {
  119.             rendezvousSocket = serverRSocket.accept();
  120.         } catch (IOException e) {
  121.             System.err.println("Accept failed.");
  122.             e.printStackTrace();
  123.             return null;
  124.         }
  125.  
  126.         //Create a thread to handle this connection.
  127.         try {
  128.             tst = new TalkServerThread(rendezvousSocket, this);
  129.             tst.start();
  130.         } catch (Exception e) {
  131.              System.err.println("Couldn't create TalkServerThread:");
  132.             e.printStackTrace();
  133.             return null;
  134.         }
  135.  
  136.         writeToStream("Successful connection. "
  137.                       + "Please wait for second applet to connect...",
  138.                       tst.os);
  139.         return tst;
  140.     }
  141.  
  142.     boolean everythingIsOK(int tstNum) {
  143.         TalkServerThread tst = tstList[tstNum];
  144.  
  145.         if (tst == null) {
  146.             if (DEBUG) {
  147.                 System.out.println("TalkServerThread #" + tstNum
  148.                                    + " is null");
  149.             }
  150.             return false;
  151.         } else {
  152.             if (tst.os == null) {
  153.                 if (DEBUG) {
  154.                     System.out.println("TalkServerThread #" + tstNum
  155.                                        + " output stream is null.");
  156.                 }
  157.                 return false;
  158.             }
  159.             if (tst.is == null) {
  160.                 if (DEBUG) {
  161.                     System.out.println("TalkServerThread #" + tstNum
  162.                                        + " input stream is null.");
  163.                 }
  164.                 return false;
  165.             }
  166.             if (tst.socket == null) {
  167.                 if (DEBUG) {
  168.                     System.out.println("TalkServerThread #" + tstNum
  169.                                        + " socket is null.");
  170.                 }
  171.                 return false;
  172.             }
  173.         }
  174.         //try {
  175.             //if ((tst.os == null) |
  176.                 //(tst.is == null) |
  177.                 //(tst.socket == null)) {
  178.                 //return false;
  179.             //}
  180.         //} catch (Exception e) {
  181.             //return false;
  182.         //}
  183.         return true;
  184.     }
  185.  
  186.     void cleanup(TalkServerThread tst) {
  187.         if (tst != null) {
  188.             try {
  189.                 if (tst.os != null) {
  190.                     tst.os.close();
  191.                     tst.os = null;
  192.                 }
  193.             } catch (Exception e) {} //Ignore errors
  194.             try {
  195.                 if (tst.is != null) {
  196.                     tst.is.close();
  197.                     tst.is = null;
  198.                 }
  199.             } catch (Exception e) {} //Ignore errors
  200.             try {
  201.                 if (tst.socket != null) {
  202.                     tst.socket.close();
  203.                     tst.socket = null;
  204.                 }
  205.             } catch (Exception e) {} //Ignore errors
  206.         }
  207.     }
  208.  
  209.     public void forwardString(String string, TalkServerThread requestor) {
  210.         DataOutputStream clientStream = null;
  211.  
  212.         if (tstList[0] == requestor) {
  213.             if (tstList[1] != null) {
  214.                 clientStream = tstList[1].os;
  215.             } else {
  216.                 if (DEBUG) {
  217.                     System.out.println("Applet #0 has a string to forward, "
  218.                                        + "but Applet #1 is gone...");
  219.                 }
  220.                 //cleanup();
  221.                 return;
  222.             }
  223.         } else {
  224.             if (tstList[0] != null) {
  225.                 clientStream = tstList[0].os;
  226.             } else {
  227.                 if (DEBUG) {
  228.                     System.out.println("Applet #1 has a string to forward, "
  229.                                        + "but Applet #0 is gone...");
  230.                 }
  231.                 //cleanup();
  232.                 return;
  233.             }
  234.         }
  235.  
  236.         if (clientStream != null) {
  237.             writeToStream(string, clientStream);   
  238.         } else if (DEBUG) {
  239.             System.out.println("Can't forward string -- no output stream.");
  240.         }
  241.     }
  242.  
  243.     public void writeToStream(String string, DataOutputStream stream) {
  244.         if (DEBUG) {
  245.             System.out.println("TalkServer about to forward data: "
  246.                                + string);
  247.         }
  248.  
  249.         try { 
  250.             stream.writeUTF(string);
  251.             stream.flush();
  252.             if (DEBUG) {
  253.                 System.out.println("TalkServer forwarded string.");
  254.             }
  255.         } catch (IOException e) {
  256.             System.err.println("TalkServer failed to forward string:");
  257.             e.printStackTrace();
  258.             //cleanup();
  259.             return;
  260.         } catch (NullPointerException e) {
  261.             System.err.println("TalkServer can't forward string "
  262.                                + "since output stream is null.");
  263.             //cleanup();
  264.             return;
  265.         }
  266.     }
  267.  
  268. }
  269.